<html>
<head><meta charset="utf-8"><title>Doubt: why lookup table for traitobjects · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html">Doubt: why lookup table for traitobjects</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236282843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236282843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Supi Mon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236282843">(Apr 27 2021 at 04:45)</a>:</h4>
<p>Its claimed that making use of TraitObjects reduces code bloat since the compiler no longer knows what implementation to use. <br>
For e.g. </p>
<p>trait DisplayMe { <br>
    fn display(&amp;self) -&gt; String<br>
 }</p>
<p>impl DisplayMe for u32{<br>
     fn display(&amp;self) -&gt; String{<br>
          format!("u32 here {}", *self)<br>
     }<br>
} </p>
<p>impl DisplayMe for String{<br>
     fn display(&amp;self) -&gt; String{<br>
          format!("String here {}", *self)<br>
     }<br>
} </p>
<p>// use case<br>
fn display_something(x: &amp;DisplayMe){<br>
     println!( "{}", x.display() );<br>
}</p>
<p>fn main(){<br>
     let x: &amp;DisplayMe = &34;<br>
     let y: &amp;DisplayMe = &amp;"Hello".to_string();</p>
<div class="codehilite"><pre><span></span><code> display_something(x);
 display_something(y);
</code></pre></div>

<p>}</p>
<p>The question is why cant the compiler infer the types(as it usually does by scanning the source) and expand the code? Or is the types inference deliberately skipped to allow dynamic dispatch during runtime ?</p>



<a name="236284387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236284387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236284387">(Apr 27 2021 at 05:11)</a>:</h4>
<p>Yes, in examples like yours, the compiler could optimize to remove the dynamic dispatch. (This is an optimization known as devirtualization, for reasons having to do with c++)</p>



<a name="236284391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236284391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236284391">(Apr 27 2021 at 05:11)</a>:</h4>
<p>In general though, after a certain point the compiler becomes unable to track the flow</p>



<a name="236284442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236284442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236284442">(Apr 27 2021 at 05:12)</a>:</h4>
<p>For example, you could have an array of trait objects, and sample from it at random</p>



<a name="236284461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236284461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236284461">(Apr 27 2021 at 05:12)</a>:</h4>
<p>or create different ones in different conditional branches</p>



<a name="236284475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236284475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236284475">(Apr 27 2021 at 05:13)</a>:</h4>
<p>These things would be forbidden for static dispatch, however are allowed for dynamic dispatch</p>



<a name="236284487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236284487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236284487">(Apr 27 2021 at 05:13)</a>:</h4>
<p>The reason why the code bloat is less is a bit more subtle though</p>



<a name="236284604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236284604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236284604">(Apr 27 2021 at 05:15)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">does_lots_of_things_to1</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">dyn</span><span class="w"> </span><span class="n">MyTrait</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Imagine this is a several hundred line function or something</span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">does_lots_of_things_to2</span><span class="o">&lt;</span><span class="n">X</span>: <span class="nc">MyTrait</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">X</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Also a several hundred line function or something</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>In the 2nd one, the compiler is <em>forced</em> to emit one version of it for each type, whereas in the first it <em>could</em> do it if it can work out the types and its heuristics say it's a good call (if its very large, they likely will not)</p>



<a name="236285022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236285022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Supi Mon <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236285022">(Apr 27 2021 at 05:21)</a>:</h4>
<p>Thanks Thom, makes sense.</p>



<a name="236330639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doubt%3A%20why%20lookup%20table%20for%20traitobjects/near/236330639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doubt.3A.20why.20lookup.20table.20for.20traitobjects.html#236330639">(Apr 27 2021 at 12:54)</a>:</h4>
<p>Hmm I don't know if forced is the right word, I wish the compiler would do more outlining<br>
<a href="https://github.com/rust-lang/rust/issues/77960">https://github.com/rust-lang/rust/issues/77960</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>